using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;

namespace BILab.Technical.BI.ModelManagement
{
	class CreateTableStatementBuilder
	{

		public CreateTableStatementBuilder()
		{
		}

		public string BuildStatement(string TableName, DataRow[] ColumnList)
		{
			StringBuilder sb = new StringBuilder();
			DataRow columnMetadata;

			/* ColumnList represents the attribute list for a given table */


			sb.Append("create table [dbo].[");
			sb.Append(TableName.Trim());

			sb.Append("] (");

			#region loop in columns
			for (int i = 0; i < ColumnList.Length; i++)
			{
				columnMetadata = ColumnList[i];

				if (i != 0)	{	sb.Append(" , ");	}
				
				sb.Append(" ");
				sb.Append(columnMetadata["SL_VariableName"].ToString());
				sb.Append(" ");


				sb.Append(columnMetadata["SL_Type"].ToString());

				if (columnMetadata["SL_Type"].ToString().Trim().ToUpper().Equals("VARCHAR"))
				{
					sb.Append("(");
					sb.Append(columnMetadata["SN_SIZE"].ToString()); 
					sb.Append(")");
				}

				if (columnMetadata["SF_Identity"].ToString().Trim().Equals("1")) // identity if usefull
				{
					sb.Append(" Identity(1,1) ");
				}

				if (columnMetadata["SF_AllowNull"].ToString().Trim().Equals("1") &&
					columnMetadata["SF_Identity"].ToString().Trim().Equals("0")
					)
				{
					sb.Append(" null ");
				}
			}
			#endregion
			sb.Append(" ) ");
			return sb.ToString();
		}
	}
}